// vue.config.js
const path = require("path")
const resolve = (dir) => path.join(__dirname, dir)
const vConsolePlugin = require("vconsole-webpack-plugin")
// 文件打包
const FileManagerPlugin = require("filemanager-webpack-plugin")

const TerserPlugin = require("terser-webpack-plugin")
// 配置打包出来的名字
const outputDirName = "dist"

module.exports = {
  publicPath: "./", // 基本路径
  outputDir: outputDirName, // 输出文件目录
  css: {
    loaderOptions: {
      //全局混入css样式变量
      scss: {
        prependData: `@import "@/styles/variables.scss";`,
      },
      less: {
        lessOptions: {
          javascriptEnabled: true,
        },
      },
    },
  },
  devServer: {
    // open: true,
    disableHostCheck: true, //映射外网
    proxy: {
      "/apis": {
        target: "http://192.168.4.206:8085/", //开发
        // target: 'http://yanshi52.bagcloud.cn/',//测试
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          "^/apis": "",
        },
      },
    },
  },
  // eslint-loader 是否在保存的时候检查
  lintOnSave: false,
  // 去除map文件
  productionSourceMap: false,
  chainWebpack: (config) => {
    config.resolve.alias
      .set("@$", resolve("src"))
      .set("assets", resolve("src/assets"))
      .set("utils", resolve("src/assets/utils"))
      .set("api", resolve("src/api"))
      .set("components", resolve("src/components"))
      .set("styles", resolve("src/styles"))
      .set("views", resolve("src/views"))
  },
  configureWebpack: (config) => {
    //生产环境去掉vconsole调试器

    let envFlag = process.env.NODE_ENV != "production"
    let pluginsDev = [
      // new vConsolePlugin({
      //   filter: [],
      //   // enable: false,
      //   enable: envFlag
      // })
    ]

    if (process.env.NODE_ENV == "production") {
      /* 生成 复制版本号文件， 并压缩文件 */
      config.plugins.push(
        new FileManagerPlugin({
          onStart: {
            delete: ["./*.zip", "./dist", "./test", "./local"],
          },
          onEnd: {
            archive: [
              {
                source: `./${outputDirName}`,
                destination: `./${outputDirName}.zip`,
              },
            ],
          },
        })
      )
      /* 移除 console */
      config.plugins.push(
        new TerserPlugin({
          terserOptions: {
            warnings: false,
            output: {
              comments: false,
            },
            compress: {
              // 删除无用的代码
              unused: true,
              // 删掉 debugger
              drop_debugger: true, // eslint-disable-line
              // 移除 console
              drop_console: true, // eslint-disable-line
              // 移除无用的代码
              dead_code: true, // eslint-disable-line
              pure_funcs: ["console.log"],
            },
          },
        })
      )
    }

    config.plugins = [...config.plugins, ...pluginsDev]
  },
}

// http://192.168.6.167:8080/#/?strSid=efe80fd8398a47609a8b02c93420dd09&roleCode=xxcuser  信息处
// http://192.168.6.167:8080/#/?strSid=6bbf3572246845d4b0b1ed64d8248d53&roleCode=msuser  领导秘书
// http://192.168.6.167:8080/#/?strSid=f0807b1a79d9480bb9bd67386bbd4639 
